package com.yy.wga.pay.service.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yy.wga.common.core.exception.BusinessException;
import com.yy.wga.pay.feign.eunms.EPayStatus;
import com.yy.wga.pay.service.event.PayCallbackEvent;
import com.yy.wga.pay.service.pojo.PayOrderDO;
import com.yy.wga.pay.service.service.PayOrderService;
import com.yy.wga.pay.service.third.AppPayNotifyDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author hongfeng
 * @date 2021/5/25
 * @desc
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PayCallbackManagerService {

    private final PayOrderService payOrderService;
    private final ApplicationEventPublisher eventPublisher;

    /**
     * 支付回调逻辑
     * @param appPayNotifyDTO 回调内容
     */
    @Transactional(rollbackFor = Exception.class)
    public void payNotify(AppPayNotifyDTO appPayNotifyDTO) {
        // 校验订单是否存在
        PayOrderDO payOrder = payOrderService.getOne(new LambdaQueryWrapper<PayOrderDO>()
                .eq(PayOrderDO::getAppKey, appPayNotifyDTO.getAppKey())
                .eq(PayOrderDO::getPayOrderNo, appPayNotifyDTO.getOutTradeNo()));
        if (payOrder == null) {
            throw new BusinessException("订单不存在");
        }

        // 校验订单状态
        if (appPayNotifyDTO.getTradeState().equals(EPayStatus.SUCCESS) || appPayNotifyDTO.getTradeState().equals(EPayStatus.FAIL)) {
            if (!payOrder.getStatus().equals(EPayStatus.WAIT.getType())) {
                log.info("订单不处于待支付状态");
                return;
            }
        } else if (appPayNotifyDTO.getTradeState().equals(EPayStatus.REFUND)) {
            if (!payOrder.getStatus().equals(EPayStatus.SUCCESS.getType()) && !payOrder.getStatus().equals(EPayStatus.WAIT.getType())) {
                throw new BusinessException("订单不处于待支付或已支付状态");
            }
        } else {
            // 其他状态不处理
            return;
        }

        // 修改支付订单
        LambdaUpdateWrapper<PayOrderDO> updateWrapper = new LambdaUpdateWrapper<PayOrderDO>()
                .eq(PayOrderDO::getId, payOrder.getId());
        updateWrapper.set(PayOrderDO::getPayOrderNo, appPayNotifyDTO.getOutTradeNo());
        updateWrapper.set(PayOrderDO::getStatus, appPayNotifyDTO.getTradeState().getType());
        updateWrapper.set(PayOrderDO::getTransactionId, appPayNotifyDTO.getTransactionId());
        if (appPayNotifyDTO.getTradeState().equals(EPayStatus.SUCCESS)) {
            updateWrapper.set(PayOrderDO::getPayTime, appPayNotifyDTO.getSuccessTime());
        }
        payOrderService.update(updateWrapper);

        // todo 支付回调
        PayCallbackEvent payCallbackEvent = new PayCallbackEvent(this);
        payCallbackEvent.setAppKey(payOrder.getAppKey());
        payCallbackEvent.setOrderNo(payOrder.getOrderNo());
        payCallbackEvent.setPayOrderNo(payOrder.getPayOrderNo());
        payCallbackEvent.setStatus(appPayNotifyDTO.getTradeState().getType());
        payCallbackEvent.setSuccessTime(appPayNotifyDTO.getSuccessTime());
        eventPublisher.publishEvent(payCallbackEvent);
    }
}
